using MicroMsg.Common.Utils;
using MicroMsg.Protocol;
using MicroMsg.Scene;
using MicroMsg.Storage;
using System;
using System.IO.IsolatedStorage;
namespace MicroMsg.Manager
{
	public class CrashLogMgr
	{
		public const int MAX_CRASH_TIMES = 10;
		public const string TAG = "CrashLogMgr";
		private const int CRASH_LOAG_SIZE = 20480;
		private const string crash_file = "crash/crash_info.xml";
		private const string crash_dir = "crash";
		private static bool hasUnhandledException = false;
		private static string strCurUserName;
		public static void onLogin(string strUserName)
		{
			CrashLogMgr.strCurUserName = strUserName;
		}
		public static void setCrash(string strException = null)
		{
			Log.smethod_5("CrashLogMgr", "setCrash in");
			CrashLogMgr.hasUnhandledException = true;
			CrashLogSettings crashLogSettings = CrashLogMgr.load();
			if (crashLogSettings.crashTimes >= 10 && (long)crashLogSettings.crashVersion == (long)((ulong)ConstantsProtocol.CLIENT_VERSION))
			{
				crashLogSettings.needReportCrashLog = false;
				CrashLogMgr.save(crashLogSettings);
				return;
			}
			crashLogSettings.needReportCrashLog = true;
			crashLogSettings.crashTimes++;
			if ((long)crashLogSettings.crashVersion != (long)((ulong)ConstantsProtocol.CLIENT_VERSION))
			{
				crashLogSettings.crashVersion = (int)ConstantsProtocol.CLIENT_VERSION;
				crashLogSettings.crashTimes = 0;
			}
			crashLogSettings.strException = strException;
			crashLogSettings.strUserName = CrashLogMgr.strCurUserName;
			CrashLogMgr.save(crashLogSettings);
		}
		public static void unSetCrash()
		{
			Log.smethod_5("CrashLogMgr", "unSetCrash in , hasUnhandledException = " + CrashLogMgr.hasUnhandledException);
			if (CrashLogMgr.hasUnhandledException)
			{
				CrashLogSettings crashLogSettings = CrashLogMgr.load();
				crashLogSettings.needReportCrashLog = false;
				CrashLogMgr.save(crashLogSettings);
				CrashLogMgr.hasUnhandledException = false;
			}
		}
		public static bool getCrashInfo(ref string crashLog, ref int crashVerion)
		{
			CrashLogSettings crashLogSettings = CrashLogMgr.load();
			Log.smethod_5("CrashLogMgr", "getCrashInfo in , settings.needReportCrashLog = " + crashLogSettings.needReportCrashLog);
			if (crashLogSettings.needReportCrashLog)
			{
				crashVerion = crashLogSettings.crashVersion;
				crashLog = Log.getLogString(20480);
				crashLog += "\n**************************************\n";
				crashLog = crashLog + "strUserName =" + crashLogSettings.strUserName + "\n";
				object obj = crashLog;
				crashLog = string.Concat(new object[]
				{
					obj,
					"crashVersion=",
					crashLogSettings.crashVersion,
					"\n"
				});
				object obj2 = crashLog;
				crashLog = string.Concat(new object[]
				{
					obj2,
					"crashNum=",
					crashLogSettings.crashTimes,
					"\n"
				});
				crashLog = crashLog + "stack =" + crashLogSettings.strException + "\n";
				if (App.unhandledExceptionTimes > 0L)
				{
					object obj3 = crashLog;
					crashLog = string.Concat(new object[]
					{
						obj3,
						"startup Exception num =",
						App.unhandledExceptionTimes,
						"\n"
					});
					if (App.lastException != null)
					{
						crashLog = crashLog + "startup Exception  = " + App.lastException.ToString() + "\n";
					}
				}
				crashLogSettings.needReportCrashLog = false;
				CrashLogMgr.clear();
				return true;
			}
			crashLog = "";
			crashVerion = 0;
			return false;
		}
		public static void SendCrashInfoByUI()
		{
			CrashInfo crashInfo = new CrashInfo();
			if (!CrashLogMgr.getCrashInfo(ref crashInfo.crashData, ref crashInfo.crashVersion))
			{
				return;
			}
			crashInfo.crashType = ECrashReportType.Blocked;
			ServiceCenter.sceneCrashReport.doScene(crashInfo);
		}
		public static void SendCrashInfo()
		{
			CrashInfo crashInfo = new CrashInfo();
			if (!CrashLogMgr.getCrashInfo(ref crashInfo.crashData, ref crashInfo.crashVersion))
			{
				return;
			}
			crashInfo.crashType = ECrashReportType.UnBlocked;
			ServiceCenter.sceneCrashReport.doScene(crashInfo);
		}
		private static void clear()
		{
			StorageIO.deleteFile("crash/crash_info.xml");
		}
		private static void save(CrashLogSettings setting)
		{
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					if (!userStoreForApplication.DirectoryExists("crash"))
					{
						userStoreForApplication.CreateDirectory("crash");
					}
				}
				StorageXml.saveToFile<CrashLogSettings>(setting, "crash/crash_info.xml");
			}
			catch (Exception ex)
			{
				Log.smethod_1("CrashLogMgr", "save setting  Exception " + ex);
			}
		}
		private static CrashLogSettings load()
		{
			CrashLogSettings crashLogSettings = StorageXml.loadFromFile<CrashLogSettings>("crash/crash_info.xml");
			if (crashLogSettings != null)
			{
				return crashLogSettings;
			}
			return new CrashLogSettings();
		}
	}
}
